package com.camemax.rabbitmq.fanout_mode;

import com.camemax.rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.lang.String;
/*
***************************
*   @Date: 2021-06-06
*   @Description: Consumer in RabbitMQ fanout mode
***************************
*/
public class FanoutMode_Consumer {
    public static void main(String[] args) {

        ConnectionFactory connectionFactory = RabbitMQUtils.createFactory();
        Channel channel = null;
        Connection connection = null;

        try {
            connection = connectionFactory.newConnection("fanout-consumer");
            channel = connection.createChannel();

            String queueName = "fanout-queue";
            channel.basicConsume(
                    queueName,
                    (consumer , message) -> System.out.println(queueName + ": " + new String( message.getBody(), StandardCharsets.UTF_8)),
                    (consumer) -> System.out.println("Consumer accepts failed!")
            );

            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            RabbitMQUtils.close(channel , connection);
        }
    }
}
